package com.hthy.futures.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hthy.futures.R;
import com.hthy.futures.bean.TradeOderStr;
import com.hthy.futures.inter.OrderBuyEvent;
import com.hthy.futures.inter.PostNewOrderClickListener;

import de.greenrobot.event.EventBus;


/**
 * <br/> Description: 提交订单
 * <br/> Author:  Mr.bingley
 * <br/> Version: 1.0
 * <br/> Date:  2016/9/30 0029 上午 09:48
 */
public class OrderSellDialog extends Dialog implements View.OnClickListener {
    Context mContxt;

    private TextView mOrderMes;
    private TextView tvPrice;
    private TextView tvNum;
    private TextView tvStopProfit;
    private TextView tvStropOutPrice;
    private PostNewOrderClickListener mPostNewOrderClickListener;
    private boolean isOtherStopLossValue = false;

    public OrderSellDialog(Context context) {
        this(context, R.style.quick_option_dialog);
    }

    public OrderSellDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContxt = context;
        View contentView = View.inflate(context, R.layout.dialog_order, null);
        initView(contentView);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.setContentView(contentView);
    }

    private void initView(View contentView) {
        mOrderMes = (TextView) contentView.findViewById(R.id.tv_buy_goodsname);
        tvPrice = (TextView) contentView.findViewById(R.id.tv_price);
        tvNum = (TextView) contentView.findViewById(R.id.tv_num);
        tvStopProfit = (TextView) contentView.findViewById(R.id.tv_stop_profit);
        tvStropOutPrice = (TextView) contentView.findViewById(R.id.tv_stop_outprice);

        Button bt_post = (Button) contentView.findViewById(R.id.bt_sure);
        bt_post.setBackgroundResource(R.drawable.btn_common_blue_style);

        ImageView img_sellStar = (ImageView) contentView.findViewById(R.id.iv_sellstar);
        img_sellStar.setBackgroundResource(R.drawable.sell_blue_star);

        Button bt_dismiss = (Button) contentView.findViewById(R.id.bt_dismiss);
        bt_dismiss.setOnClickListener(this);
        bt_post.setOnClickListener(this);

    }

    /**
     * 从新订单页面设置相关的数据
     *
     * @param orderStr
     */
    public void setOrderData(TradeOderStr orderStr) {
        mOrderMes.setText(mContxt.getString(R.string.sell) + ":" + orderStr.tradeCode);
        tvPrice.setText("价格" + orderStr.currentPrice);
        tvNum.setText("手数:" + orderStr.tradeBuyCount);
        tvStopProfit.setText("止盈价:" + orderStr.trade_dicount_price);
        if (isOtherStopLossValue) {
            tvStropOutPrice.setText("止损价:" + "");
        } else {
            tvStropOutPrice.setText("止损价:" + orderStr.trade_stop_price);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.CENTER);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = ViewGroup.LayoutParams.MATCH_PARENT;
        p.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(p);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.bt_sure:  //
                // EventBus.getDefault().post(new OrderBuyEvent(1));
                mPostNewOrderClickListener.post();
                break;
            case R.id.bt_dismiss:
                dismiss();
                break;
        }
        dismiss();
    }

    public void setPostNewOrderClickListner(PostNewOrderClickListener postNewOrderClickListener) {
        mPostNewOrderClickListener = postNewOrderClickListener;
    }

    public void setIsOtherStopLossValue(boolean isOtherStopLossValue) {
        this.isOtherStopLossValue = isOtherStopLossValue;
    }
}
